import numpy
from PyQt4 import QtCore



class KHolding(QtCore.QObject):
	class HoldType(QtCore.QObject):
		LONG=1
		SHORT=2
		NEUTRAL =3
	''' represent an stock holding maitained by KPosition'''
	def __init__(self,parent,symbol,holdtype,price,quant):
		QtCore.QObject.__init__(self,parent)
		self.__symbol=symbol
		self.__holdtype=holdtype
		self.avgprice=price
		self.quant=quant
		self.curmarketvalue=price # current market value of this holding
		
	def count(self):
		return self.quant
	
	def cost(self):
		# the cost to create this holding
		return self.avgprice*self.quant
	
	def Type(self):
		return self.__holdtype
	
	def Value(self):
		# the total market value of current holding
		return self.curmarketvalue
	
	def TotalValue(self):
		return self.curmarketvalue*self.quant
	
	
	def UpdateMarketValue(self,newprice):
		# update market value of current holding
		self.curmarketvalue=newprice

		
		
	def AddLongToHolding(self,price,quant):
		
		curprice=self.avgprice*self.quant
		newprice=curprice+price*quant
		self.quant+=quant
		self.avgprice=newprice/self.quant
		self.__holdtype=KHolding.HoldType.LONG
		return True
	
	def ReduceLongFromHolding(self,quant):
		if self.quant<quant:
			#can't reduce
			return False
		else:
			self.quant-=quant
			if self.quant==0:
				self.__holdtype=KHolding.HoldType.NEUTRAL
			return True